package com.centling.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.centling.business.CustomerBean;
import com.centling.business.OrderInfoImportVO;
import com.centling.business.OrderVO;
import com.centling.dao.ICustomerDAO;
import com.centling.dao.IOrderDAO;
import com.centling.dao.IUtilsDAO;
import com.centling.service.IOrderService;
import com.centling.util.LocalUtils;

@Service
@Transactional
public class OrderService implements IOrderService{

	@Resource
	private ICustomerDAO customerDAO;
	
	@Resource
	private IOrderDAO orderDAO;
	
	@Resource
	private IUtilsDAO utilsDAO;
	
	@Override
	public String getNoDeliveryOrderIDs() {
		StringBuffer returnStr = new StringBuffer("");
		List<String> list = orderDAO.getNoDeliveryOrderIDs();
		if (list!=null&&!list.isEmpty()){
			for (String str : list){
				returnStr.append(returnStr.toString().contains(str)?"":str+",");
			}
		}
		return returnStr.toString();
	}
	
	@Override
	public Boolean processOrdersBL(List<OrderVO> volist,
			List<String[]> orderstatuslist) {
		/**
		 * 第一部分处理：	新增
		 */
		if (volist!=null&&!volist.isEmpty())
		for (OrderVO vo : volist){
			//	1	 门店定制客户以“客户信息”中的“电话”作为“客户”与“订单”的匹配项
			if (vo.getFphone()!=null&&vo.getFphone().trim().length()!=0){
				Integer customerid	= customerDAO.findFItemIDByPhone(vo.getFphone());
				Integer orderid		= utilsDAO.getICMaxNum("t_RPContract");
				if (customerid!=-1 && orderid!=-1){
					Integer fCurrencyID = customerDAO.getFCurrencyIDByCustomreID(customerid);
					String theFirstDayOfLastMonth = LocalUtils.getTheFirstDayOfLastMonth(new Date());
					String fExchangeRate = utilsDAO.getFExchangeRateByFCurrencyID(fCurrencyID, theFirstDayOfLastMonth);
					OrderInfoImportVO tempvo = customerDAO.findPartCustomerInfoByF_other_customer_code(vo.getF_user_name());
					orderDAO.save(vo, customerid, orderid, fCurrencyID, fExchangeRate, tempvo.getFemployee(), tempvo.getFdepartment());
					customerDAO.updateFlastTradeDate(customerid);
					utilsDAO.setICMaxNum("t_RPContract");
				} else if (customerid==-1 && orderid!=-1){
					CustomerBean cb = new CustomerBean();
					/** 
					 * 已跟刘立鑫电话确认，如果用手机检索不到客户，就插入一个新的客户
					 * 向新客户中插入五个字段--手机号、客户姓名、红领客户编码（手机号）（CRM客户编码（手机号）） 、来源系统
					 */
					cb.setfPhone(vo.getFphone());
					cb.setfName(vo.getfName()==null?"":vo.getfName());
					cb.setF_other_customer_code(vo.getFphone());
					cb.setF_system_source(vo.getF_customer_source()==null?"":vo.getF_customer_source());
					cb.setF_customer_source(vo.getF_customer_source()==null?"":vo.getF_customer_source());
					cb.setF_customer_type("是");
					int fItemID = utilsDAO.getICMaxNum("t_item");
					if (fItemID!=-1){
						OrderInfoImportVO tempvo = customerDAO.findPartCustomerInfoByF_other_customer_code(vo.getF_user_name());
						customerDAO.save(cb, fItemID, tempvo.getfItemID());
						utilsDAO.processT_itemWhenInsertCustomer(fItemID, cb.getF_other_customer_code(), cb.getfName());
						utilsDAO.setICMaxNum("t_item");
						customerid	= customerDAO.findFItemIDByPhone(vo.getFphone());
						if (customerid!=-1){
							Integer fCurrencyID = customerDAO.getFCurrencyIDByCustomreID(customerid);
							String theFirstDayOfLastMonth = LocalUtils.getTheFirstDayOfLastMonth(new Date());
							String fExchangeRate = utilsDAO.getFExchangeRateByFCurrencyID(fCurrencyID, theFirstDayOfLastMonth);
							orderDAO.save(vo, customerid, orderid, fCurrencyID, fExchangeRate, tempvo.getFemployee(), tempvo.getFdepartment());
							customerDAO.updateFlastTradeDate(customerid);
							utilsDAO.setICMaxNum("t_RPContract");
						}
					}
				}
			}
			//	2	其他定制客户以“基本信息”中的“用户名”作为“客户”与“订单”的匹配项
			else if (vo.getF_user_name()!=null&&vo.getF_user_name().trim().length()!=0){
				OrderInfoImportVO tempvo = customerDAO.findPartCustomerInfoByF_other_customer_code(vo.getF_user_name());
				Integer customerid	= tempvo.getfItemID();
				Integer orderid		= utilsDAO.getICMaxNum("t_RPContract");
				if (customerid!=-1 && orderid!=-1){
					Integer fCurrencyID = customerDAO.getFCurrencyIDByCustomreID(customerid);
					String theFirstDayOfLastMonth = LocalUtils.getTheFirstDayOfLastMonth(new Date());
					String fExchangeRate = utilsDAO.getFExchangeRateByFCurrencyID(fCurrencyID, theFirstDayOfLastMonth);
					orderDAO.save(vo, customerid, orderid, fCurrencyID, fExchangeRate, tempvo.getFemployee(), tempvo.getFdepartment());
					customerDAO.updateFlastTradeDate(customerid);
					utilsDAO.setICMaxNum("t_RPContract");
				}
			}
		}
		/**
		 * 第二部分处理：	修改
		 */
		if (orderstatuslist!=null&&!orderstatuslist.isEmpty())
		for (String temp[] : orderstatuslist){
			orderDAO.updateOrder(temp[0], temp[1]);
		}
		return true;
	}
}
